#include <cv.h>
#include <highgui.h>
#include "abstractProbabilityTransformer.hpp"
#include "abstractFilter.hpp"
#include "resourceTracker.h"
#include "faceDetector.h"

//this looks up the location of the gtest files in header hiDependenciesForTestFramework.h
//TODO : Design choice? currently I'll keep this dependency in a very specific header file
// as changing it should cause minimal recompilation.

#include "hiDependenciesForTestFramework.h"
#include HI_DEPENDENCY_GTEST_HEADER

//NOTE: hiDependenciesForTestFramework also sets HI_TEST_VISUALISATION
//which if set will make tests compile to show images of the things they test.

using namespace cv;

namespace {
	
	// The fixture for testing class PerPixelProbabilityTransformerTest.
	class PerPixelProbabilityTransformerTest : public ::testing::Test {
	public:
		
		AbstractProbabilityTransformer *probTrans;
		ResourceTracker* rt;
	protected:
		
		Mat hsvFullSize;
		//		
		//		// Objects declared here can be used by all tests in the test case for PerPixelProbabilityTransformer.
		
		virtual void SetUp() {
			
			rt = new ResourceTracker( "../config/runtimeSettings/cfg.xml" );
			rt->loadCategory("TestData");
			
			//Mat testImg = rt->getMat("facetest1");
			
			Mat testImg = rt->getMat("handL_back_tom");
			
			cvtColor(testImg, hsvFullSize, CV_BGR2HSV);
			
			//get HSV version
			FaceDetector* fd = new FaceDetector(1.0);
			
			vector<Rect>* faces = fd->detectFace(testImg);
			
			ASSERT_TRUE(!faces->empty());
			
			Rect faceRect = faces->front();
			

			Rect narrowFaceRect = Rect( faceRect.x+faceRect.width/6, faceRect.y, 2*faceRect.width/3, faceRect.height);
			
#ifdef HI_TEST_VISUALISATION
			                      //Draw full face rect 
			                      rectangle( testImg, Point( faceRect.x, faceRect.y ), //WAS bug : cvRectangle instead
			                                        Point( faceRect.x+faceRect.width, faceRect.y + faceRect.height ),
			                                        Scalar::all(0),
			                                        2);           
			                      //Draw narrow face rect
			                      rectangle( testImg, Point( narrowFaceRect.x, narrowFaceRect.y ), //WAS bug : cvRectangle instead
			                                        Point( narrowFaceRect.x+narrowFaceRect.width, narrowFaceRect.y + narrowFaceRect.height ),
			                                        Scalar::all(100),
			                                        2);
			
			namedWindow( "Face detection", 1 );
			imshow( "Face detection", testImg );
			waitKey();
#endif
			
			Mat hsvFace( hsvFullSize, narrowFaceRect );
			
			Ptr< AbstractFilter > filterPtr(new OpenFilter(1));
			
			probTrans = new PerPixelProbabilityTransformer( filterPtr );
			
			probTrans->calcAndSetHist(hsvFace);
		}
		
		
		virtual void TearDown() {
			delete rt;
			delete probTrans;
		}

	};
	
}  // namespace

TEST_F(PerPixelProbabilityTransformerTest, getBackProjectionDoesNotBreak ) {
	
	Mat backProj = Mat::zeros(hsvFullSize.cols, hsvFullSize.rows, CV_8UC1);	
	probTrans->getBackProjection(hsvFullSize, backProj);
	
#ifdef HI_TEST_VISUALISATION
	namedWindow( "BackProj", 1 );
	imshow( "BackProj", backProj );
	waitKey();
	
	//FIX cleanup?
#endif
	
	
	//TODO better test
}

//use linking to gtest_main.a instead
//int main(int argc, char **argv) {
//	::testing::InitGoogleTest(&argc, argv);
//	return RUN_ALL_TESTS();
//}